import Object.BingPic;
import Object.Constant;
import Util.HttpUtil;
import Object.Pic;
import Object.MyRunnable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author jklu
 * Bing爬虫的相关函数，与Baidu类似
 */
public class BingSpider {

    private ExecutorService executor;
    /**
     * 基于API，设置每一页的请求链接
     * @param word  搜索关键字
     * @param start 页的起始地址
     * @param count 一页包含的图片数量
     * @param color 颜色筛选
     * @return      每一页的请求链接
     */
    private String setUrl(String word,int start,int count,String color){
        String result = Constant.API_BING;
        result = result.replace("{word}",word);
        result = result.replace("{begin}",String.valueOf(start));
        result = result.replace("{count}",String.valueOf(count));
        result = result.replace("{color}",color);
        System.out.println(result);
        return result;
    }

    /**
     * 设置颜色筛选
     * @param color 自定义的颜色选项
     * @return      服务器接受的颜色选项
     */
    private String setColor(int color){
        String result ="";
        //颜色(0全部颜色 1红色 2橘色 4黄色 8绿色 16青色 32蓝色 64紫色 128粉色 256棕色 512灰色 1024黑色 2048白色 4096彩色 8192黑白)
        switch (color){
            case 0:
                break;
            case 1:
                result = "filterui:color2-FGcls_RED";
                break;
            case 2:
                result = "filterui:color2-FGcls_ORANGE";
                break;
            case 4:
                result = "filterui:color2-FGcls_YELLOW";
                break;
            case 8:
                result = "filterui:color2-FGcls_GREEN";
                break;
            case 16:
                result = "filterui:color2-FGcls_TEAL";
                break;
            case 32:
                result = "filterui:color2-FGcls_BLUE";
                break;
            case 64:
                result = "filterui:color2-FGcls_PURPLE";
                break;
            case 128:
                result = "filterui:color2-FGcls_PINK";
                break;
            case 256:
                result = "filterui:color2-FGcls_BROWN";
                break;
            case 512:
                result = "filterui:color2-FGcls_GRAY";
                break;
            case 1024:
                result = "filterui:color2-FGcls_BLACK";
                break;
            case 2048:
                result = "filterui:color2-FGcls_WHITE";
                break;
            case 4096:
                result = "filterui:color2-color";
                break;
            case 8192:
                result = "filterui:color2-bw";
                break;
            default:
                break;
        }
        return result;
    }

    /**
     * 用户使用
     * 下载一个BingPic对象中所有Pic对象
     * @param bingPic   要下载的BingPic对象
     * @param useProxy  是否需要使用代理，用户设置
     */
    protected void downloadBingPic(BingPic bingPic,boolean useProxy){
        executor = Executors.newCachedThreadPool();
        for (Pic p:bingPic.getPicList()) {
            executor.execute(new MyRunnable(p,4,useProxy));
        }
    }

    /**
     * 用户使用
     * 获取一页数据的对象，即一个BingPic对象
     * @param word  搜索关键字
     * @param start 页的起始地址，用于图片序号的设置
     * @param count 一页包含的图片数量
     * @param color 自定义的颜色筛选
     * @return      一个BingPic对象，包含一页中所有图片对象的集合
     */
    protected BingPic getBingPic(String word,int start,int count,int color){
        return new BingPic(HttpUtil.getDocument(setUrl(word, start, count,setColor(color)),false),start);
    }
}
